<template>
  <addEditBaseAchievementClassMetricsView ref="addEditBaseAchievementClassMetricsView" />
  <addFileView ref="addFileView" />
  <el-main>
    <div class="teacher">
      <div class="app-table">
        <div class="app-body">
          <el-form :inline="true" class="demo-form-inline" v-model="form">
            <el-form-item label="所在年级" :label-width="formLabelWidth" prop="gradeId">
              <el-select clearable v-model="form.gradeId" autocomplete="off" placeholder="请选择所在年级" style="width:190px;">
                <el-option v-for="gradeId in gradeIds" :key="gradeId" :value="gradeId"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="科目" :label-width="formLabelWidth" prop="subjectName">
              <el-select clearable v-model="form.subjectName" autocomplete="off" placeholder="请选择科目"
                style="width:190px;">
                <el-option v-for="subjectName in subjectNames" :key="subjectName" :value="subjectName"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit" @keyup.enter="searchEnter">查询</el-button>
              <!-- <el-button type="success" @click="add">添加</el-button> -->
              <el-button type="primary" @click="exportExcel" plain>下载模版</el-button>
              <el-button type="success" @click="importExcel" plain>导入指标<el-icon class="el-icon--right">
                  <Upload />
                </el-icon></el-button>
            </el-form-item>
          </el-form>
          <el-table size="small" :data="list" style="width: 100%" border :row-class-name="tableRowClassName">
            <el-table-column prop="id" type="index" label="序号" width="90" />
            <el-table-column prop="subjectName" label="学科" />
            <el-table-column prop="gradeId" width="90" label="年级" />
            <el-table-column prop="classId" width="90" label="班级" />
            <!-- <el-table-column prop="campus" label="校区编号"/> -->
            <el-table-column prop="index985" label="985指标" />
            <el-table-column prop="index211" label="211指标" />
            <el-table-column prop="indexKey" label="重本指标" />
            <el-table-column prop="indexGaokao" label="一本指标" />
            <el-table-column prop="calculationMethod" label="综合贡献率计算方法" />
            <el-table-column label="操作" width="140px">
              <template style="display: block" #default="scope">
                <el-button size="small" type="success" @click="editBaseAchievementClassMetrics(scope.row)">编辑
                </el-button>
                <el-button size="small" type="danger" @click="deleteBaseAchievementClassMetrics(scope.row.id)">删除
                </el-button>
              </template>
            </el-table-column>
            <template v-slot:empty>
              <el-empty description="暂无数据"></el-empty>
            </template>
          </el-table>
          <el-config-provider :locale="locale">
            <el-pagination :total="total" :page-size="limit" :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
              layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
              @current-change="changeCurrentPage">
            </el-pagination>
          </el-config-provider>
        </div>
        <bottomView></bottomView>
      </div>
    </div>
  </el-main>
</template>
<script>
import addEditBaseAchievementClassMetricsView from "@/components/achievementAnalysis/addEditBaseAchievementClassMetrics.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";

export default {
  name: "BaseAchievementClassMetricsListView",
  data() {
    return {
      type: 0,
      title: "",
      page: 0,
      pageSize: 20,
      total: 0,
      limit: 20,
      list: [],
      gradeIds: [],
      subjectNames: ["总成绩", "语文", "数学", "英语", "物理", "化学", "生物", "历史", "地理", "政治"],
      form: {
        gradeId: "",
        subjectName: ""
      },
    };
  },
  components: {
    addEditBaseAchievementClassMetricsView,
    addFileView,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    Upload, bottomView,
  },
  setup() {
    return {
      locale: zhCn,
    };
  },
  mounted() {
    this.getGradeIds();

    this.getBaseAchievementClassMetricsList();
  },
  watch: {
    $route() {
      this.getBaseAchievementClassMetricsList();
    },
  },
  methods: {
    getGradeIds() {
      let date = new Date();
      let nowYear = parseInt(date.getFullYear());
      let nowMonth = parseInt(date.getMonth());
      let gradeIds = [];
      if (nowMonth > 6) {
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
        gradeIds.push(nowYear);
      } else {
        gradeIds.push(nowYear - 3);
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
      }
      this.gradeIds = gradeIds;
    },
    searchEnter: function (e) {
      // 使用 which 和 keyCode 属性来解决兼容问题
      var keyCode = window.event ? e.keyCode : e.which;
      var val = e.target.value;
      if (keyCode == 13 && val) {
        this.getBaseAchievementClassMetricsList();
      }
    },
    add() {
      this.$refs.addEditBaseAchievementClassMetricsView.title =
        "添加考试班级指标表";
      this.$refs.addEditBaseAchievementClassMetricsView.personnelType =
        this.type;
      this.$refs.addEditBaseAchievementClassMetricsView.dialogFormVisible = true;
    },
    editBaseAchievementClassMetrics(obj) {
      this.$refs.addEditBaseAchievementClassMetricsView.title = "修改信息";
      this.$refs.addEditBaseAchievementClassMetricsView.getBaseAchievementClassMetricsById(
        obj.id
      );
      this.$refs.addEditBaseAchievementClassMetricsView.dialogFormVisible = true;
    },
    deleteBaseAchievementClassMetrics(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService(
            "/javaApi/baseAchievementClassMetrics/deleteById",
            "id=" + id,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success("删除成功");
              this.getBaseAchievementClassMetricsList();
            } else {
              ElMessage.error("删除失败，请重试！");
            }
          });
        })
        .catch(() => { });
    },
    reloadData() {
      this.page = 1;
      this.getBaseAchievementClassMetricsList();
    },
    importExcel() {
      this.$refs.addFileView.dialogFormVisible = true;
      this.$refs.addFileView.title = "导入指标";
      this.$refs.addFileView.uploadApiUrl =
        "/javaApi/baseAchievementClassMetrics/importExcel";
    },
    exportExcel() {
      httpService(
        "/javaApi/baseAchievementClassMetrics/exportExcel",
        {
          gradeId: this.form.gradeId,
          subjectName: this.form.subjectName,
        },
        "get"
      ).then((data) => {
        if (data.code == 200) {
          window.open(data.msg, "_blank");
        }
      });
    },
    onSubmit() {
      this.getBaseAchievementClassMetricsList();
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      this.limit = size;
      this.getBaseAchievementClassMetricsList();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getBaseAchievementClassMetricsList();
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },
    getBaseAchievementClassMetricsList() {
      httpService(
        "/javaApi/baseAchievementClassMetrics/list/" +
        this.page +
        "/" +
        this.pageSize,
        {
          gradeId: this.form.gradeId,
          subjectName: this.form.subjectName,

        },
        "get"
      ).then((data) => {
        this.list = data.list;
        this.total = data.total;
        this.pageCount = data.pages;
      });
    },
  },
};
</script>
<style scoped>
.el-main {
  padding: 0px;
}

.teacher-content {
  width: 100%;
}

.app-table {
  background: #eff3f5;
  padding: 10px;
}

.app-search {
  padding-top: 20px;
  border-top: 1px solid #dedede;
  margin-top: 20px;
}

.app-separator {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
}

.app-body {
  background: white;
  padding: 20px 20px 10px 20px;
  margin-bottom: 10px;
  min-height: calc(100vh - 243px);
}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 10px;
}
</style>
